package com.guigui.gulimall.product.controller;

import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.R;
import com.guigui.gulimall.product.dto.spuinfo.SpuSaveDto;
import com.guigui.gulimall.product.entity.SkuInfoEntity;
import com.guigui.gulimall.product.entity.SpuInfoEntity;
import com.guigui.gulimall.product.service.SkuInfoService;
import com.guigui.gulimall.product.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * spu信息
 *
 * @author xin
 * @email 806748502@qq.com
 * @date 2021-07-23 15:09:02
 */
@RestController
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 上架商品
     */
    @PostMapping("/{spuId}/up")
    public R up(@PathVariable("spuId") Long spuId){

        spuInfoService.up(spuId);

        return R.ok();
    }

    @GetMapping("/spuInfo/{skuId}")
    public SpuInfoEntity getSpuInfoBySkuId(@PathVariable("skuId") Long skuId) {
        SkuInfoEntity byId = skuInfoService.getById(skuId);
        return spuInfoService.getById(byId.getSpuId());
    }

    /**
     * map参数
     * status:
     * key: 荣耀
     * brandId: 12
     * catelogId: 225
     * page: 1
     * limit: 10
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:spuinfo:list")
    public R listByCondition(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveDto spuSaveDto){


		spuInfoService.saveSpuDto(spuSaveDto);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
